<?php
//员工
namespace app\crm\model;

use think\Db;

class Downstream extends \ShopModel
{
    protected $table = 'sh_crm_staff';
    protected $pk = 'staff_id';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'create_at';
    public function DownstreamDb(){
        return Db::name('crm_staff');
    }


    public function getCustodianTrans($status){
        $all_status = array(
            0 => '否',
            1 => '是',
        );
        return !empty($all_status[$status]) ? $all_status[$status] : '未知';
    }
    public function getLoginTrans($source){
        $all_status = array(
            0 => '不允许',
            1 => '允许',
         );
        return !empty($all_status[$source]) ? $all_status[$source] : '未知';
    }
    public function search($params){
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 10;
        $where = [];
        $params['is_sublevel'] = $params['is_sublevel'] == 'true' ? 1 : 0;
        if($params['is_sublevel']){
           $Department = new \app\crm\controller\Department();
           $department_id_arr =  $Department->getDepartmentAffiliate($params['department_id'],1);
            $where['staff.department_id'] =  ['in',$department_id_arr];
        }else{
            $where['staff.department_id'] = $params['department_id'];
        }
        if(!empty($params['query'])){
            $where['staff.staff_name | staff.phone | staff.qq | staff.email '] = ['like','%'.$params['query'].'%'];
        }
        if($params['sys_custodian']!=2){
            $where['staff.sys_custodian'] = $params['sys_custodian'];
        }
        if($params['is_login']!=2){
            $where['staff.is_login'] = $params['is_login'];
        }
        if($params['login_lock']!=2){
            $where['staff.login_lock'] = $params['login_lock'];
        }
        if($params['attention_type']!=2){
            $where['staff.attention_type'] = $params['attention_type'];
        }
        if($params['dimission_type']!=2){
            $where['staff.dimission_type'] = $params['dimission_type'];
        }
        $where['staff.is_downstream'] = 0;
        //排除上下游互动
        $Sys = new Sys();
        $did = $Sys->where('id',1)->value('crm_sxyhd_did');
        $where['department_id']=['neq',$did];
        $data = $this->alias('staff')
            ->join('sh_crm_department department','staff.department_id = department.department_id')
            ->field('staff.staff_id,staff.staff_name,staff.head_img, department.department_name,staff.phone, staff.entry_time, staff.sys_custodian, staff.is_login, staff.attention_type')
            ->where($where)
            ->page($page,$pageSize)
            ->order('staff.staff_id desc')
            ->select();

        foreach($data as &$v){
            $v['sys_custodian'] = $this->getCustodianTrans($v['sys_custodian']);
            $v['is_login'] = $this->getLoginTrans($v['is_login']);
            $v['entry_time'] = date('Y-m-d',$v['entry_time']);

        }

        //获取分页数据
        $count = $this->alias('staff')
            ->join('sh_crm_department department','staff.department_id = department.department_id')
            ->field('staff.staff_id,staff.staff_name,staff.head_img, department.department_name,staff.phone, staff.entry_time, staff.sys_custodian, staff.login_lock, staff.attention_type')
            ->where($where)

            ->order('staff.staff_id desc')
            ->count();

        $pager = [
            'total' => $count,
            'page' => $page,
            'pageSize' => $pageSize
        ];



        return [
            'staff' => $data,
            'pager' => $pager
        ];

    }

    //简单筛选
    public function easySearch($params){
        $where = [];
        if(!empty($params['query'])){
            $where['staff.staff_name | staff.phone | staff.qq | staff.email '] = ['like','%'.$params['query'].'%'];
        }
        $where['staff.is_downstream'] = 0;
        $data = $this->alias('staff')
            ->join('sh_crm_department department','staff.department_id = department.department_id')
            ->field('staff.staff_id,staff.staff_name,staff.head_img,department.department_name')
            ->where($where)
            ->order('staff.staff_id desc')
            ->select();
        foreach($data as &$v){
            $v['is_checked'] = false;
        };
        return $data;
    }
}
